package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.annotation.Arithmetic;
import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarRangeGasOpenPercent;
import com.navinfo.platform.trip.common.arithmetic.enums.RangeGasOpenPercentEnum;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

import java.util.Optional;

/**
 * 油门开合度算法.<br>
 * 依赖{@link IdleParking}的怠速状态.<br>
 * 思路是累加油门开合度，累计数量，行程结束的时候除一下<br>
 * 输出：统计<br>
 */
@Arithmetic(name = "平均油门开度")
public class RangeGasOpenPercent extends AbstractArithmetic<VarRangeGasOpenPercent> {

    @Override
    public void calculate() {
        logger.debug("进入平均油门开度算法.");
        VarRangeGasOpenPercent var = super.getObj();
        CommonData commonData = drivingData.getCommonData();

        int duration = commonData.getDffTime();
        int accPedalPos = (int)commonData.getAcceleratorPosition();
        if (commonData.isWork()) {
            if (RangeGasOpenPercentEnum.RANGE_0.getRange().contains(accPedalPos)) {
                var.setGasOpenPercent0Duration( Optional.ofNullable(var.getGasOpenPercent0Duration()).orElse(0) + duration);
            } else if (RangeGasOpenPercentEnum.RANGE_10.getRange().contains(accPedalPos)) {
                var.setGasOpenPercent10Duration( Optional.ofNullable(var.getGasOpenPercent10Duration()).orElse(0)  + duration);
            } else if (RangeGasOpenPercentEnum.RANGE_20.getRange().contains(accPedalPos)) {
                var.setGasOpenPercent20Duration( Optional.ofNullable(var.getGasOpenPercent20Duration()).orElse(0)  + duration);
            } else if (RangeGasOpenPercentEnum.RANGE_30.getRange().contains(accPedalPos)) {
                var.setGasOpenPercent30Duration( Optional.ofNullable(var.getGasOpenPercent30Duration()).orElse(0)  + duration);
            } else if (RangeGasOpenPercentEnum.RANGE_40.getRange().contains(accPedalPos)) {
                var.setGasOpenPercent40Duration( Optional.ofNullable(var.getGasOpenPercent40Duration()).orElse(0)  + duration);
            } else if (RangeGasOpenPercentEnum.RANGE_50.getRange().contains(accPedalPos)) {
                var.setGasOpenPercent50Duration( Optional.ofNullable(var.getGasOpenPercent50Duration()).orElse(0)  + duration);
            } else if (RangeGasOpenPercentEnum.RANGE_60.getRange().contains(accPedalPos)) {
                var.setGasOpenPercent60Duration( Optional.ofNullable(var.getGasOpenPercent60Duration()).orElse(0)  + duration);
            } else if (RangeGasOpenPercentEnum.RANGE_70.getRange().contains(accPedalPos)) {
                var.setGasOpenPercent70Duration( Optional.ofNullable(var.getGasOpenPercent70Duration()).orElse(0)   + duration);
            } else if (RangeGasOpenPercentEnum.RANGE_80.getRange().contains(accPedalPos)) {
                var.setGasOpenPercent80Duration( Optional.ofNullable(var.getGasOpenPercent80Duration()).orElse(0)  + duration);
            } else if (RangeGasOpenPercentEnum.RANGE_90.getRange().contains(accPedalPos)) {
                var.setGasOpenPercent90Duration( Optional.ofNullable(var.getGasOpenPercent90Duration()).orElse(0)   + duration);
            } else if (RangeGasOpenPercentEnum.RANGE_99.getRange().contains(accPedalPos)) {
                var.setGasOpenPercent99Duration( Optional.ofNullable(var.getGasOpenPercent99Duration()).orElse(0)  + duration);
            } else if (RangeGasOpenPercentEnum.RANGE_100.getRange().contains(accPedalPos)) {
                var.setGasOpenPercent100Duration( Optional.ofNullable(var.getGasOpenPercent100Duration()).orElse(0)  + duration);
            }
        }

        var.setGasOpenPercentSum(var.getGasOpenPercentSum() + accPedalPos);
        var.setGasOpenPercentCount(var.getGasOpenPercentCount() + 1);

        logger.debug("平均油门开度算法结束.");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarRangeGasOpenPercent var = super.getObj();
        statisticDataOut.setGasOpenPercent0Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent0Duration()));
        statisticDataOut.setGasOpenPercent10Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent10Duration()));
        statisticDataOut.setGasOpenPercent20Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent20Duration()));
        statisticDataOut.setGasOpenPercent30Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent30Duration()));
        statisticDataOut.setGasOpenPercent40Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent40Duration()));
        statisticDataOut.setGasOpenPercent50Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent50Duration()));
        statisticDataOut.setGasOpenPercent60Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent60Duration()));
        statisticDataOut.setGasOpenPercent70Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent70Duration()));
        statisticDataOut.setGasOpenPercent80Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent80Duration()));
        statisticDataOut.setGasOpenPercent90Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent90Duration()));
        statisticDataOut.setGasOpenPercent99Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent99Duration()));
        statisticDataOut.setGasOpenPercent100Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent100Duration()));
        statisticDataOut.setGasOpenPercentSum((long)var.getGasOpenPercentSum());
        statisticDataOut.setGasOpenPercentCount(var.getGasOpenPercentCount());
    }
}
